package com.longdaji.decoration.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.longdaji.decoration.R;

/**
 * Created by longtao on 2018/2/7.
 */

public class ArcView extends View {
    private  int mArcColor;
    private int mWidth;
    private int mHeight;
    /**
     * 弧形高度
     */
    private int mArcHeight;
    /**
     * 背景颜色
     */
    private int mBgColor;
    private Paint mPaint;
    private Context mContext;

    public ArcView(Context context) {
        this(context, null);
    }

    public ArcView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcView);
        mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcView_arcHeight, 0);
        mBgColor=typedArray.getColor(R.styleable.ArcView_bgColor, getResources().getColor(R.color.white));
        mArcColor=typedArray.getColor(R.styleable.ArcView_arcColor, getResources().getColor(R.color.line_color));
        mContext = context;
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mBgColor);

        Rect rect = new Rect(0, 0, mWidth, mHeight);
        canvas.drawRect(rect, mPaint);

        RectF oval = new RectF( mWidth/2-mArcHeight, -mArcHeight,
        mWidth/2+mArcHeight, mArcHeight);

        mPaint.setColor(mArcColor);
        canvas.drawArc(oval,360,180,false,mPaint);//画圆弧，这个时候，绘制没有经过圆心
//         mPaint.setColor(Color.BLACK);
//        RectF oval1 = new RectF( mWidth/2-mArcHeight-10, -mArcHeight-10,
//                mWidth/2+mArcHeight-10, mArcHeight-10);
//        canvas.drawOval(oval1,mPaint);
        //canvas.drawArc(oval1, 360, 0, false, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        }
        setMeasuredDimension(mWidth, mHeight);
    }
}